<?php
	include "connection-provider.php";
		session_start();
	//$typeSessionId 
	$typeId = $_SESSION['typeIdSession'];
	
	$result = mysqli_query($con,"SELECT * FROM question_table WHERE typeId = $typeId");
		$countCorrect = 0;
		
		//echo $typeId;
		
	while($row = mysqli_fetch_array($result))
	{	
		$finalResult = "Wrong";
		if($row['correctAnswer'] == $_POST[$row['questionNumber']]){
			$finalResult = "Correct";
			$countCorrect++;
		}
		//1 need to replace with proper student id
		$studentId = $_SESSION['studentId'];
		$sqlQuery = "insert into `answer_sheet` (`StudentID`, `QuesID`, `Answer`, `Result`, `typeId`) values ('$studentId',".$row['questionNumber'].", \"".$_POST[$row['questionNumber']]."\",\"".$finalResult."\",'$typeId')";
		mysqli_query($con, $sqlQuery);
	}	
		
	
/*	
		//1 need to replace with proper student id
		$sqlQuery = "insert into `total_marks_table` (`student_id`, `total_marks`, `typeId`) values (\"2\",'$countCorrect','$typeId')";
		mysqli_query($con, $sqlQuery);
		
*/
		
		//1 need to replace with proper student id
		echo $countCorrect;
		$studentId = $_SESSION['studentId'];
		$q = "UPDATE `total_marks_table` SET total_marks='$countCorrect', submit_status='submitted' WHERE student_id=$studentId && typeId=$typeId";
		mysqli_query($con, $q);
	
	$count = 0;
	$result = mysqli_query($con,"SELECT * FROM `total_marks_table` WHERE student_id='$studentId' && submit_status='submitted'");		
	
			while($row = mysqli_fetch_array($result))
				{
					//echo $row['student_id'];
					$count++;
				}
	
	if($count ==2)
	{
		header( "Location: logout.php" );
	}
	else
	{
		header( "Location: select-type.php?typeId=$typeId" );
	}
	
	
		
		
	// code to redirect to the proper page
?>